package com.alvis.media.service.impl;

import com.alvis.media.domain.FileInfo;

import com.alvis.media.repository.FileInfoMapper;
import com.alvis.media.repository.MediaBaseMapper;
import com.alvis.media.service.FileInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FileInfoServiceImpl extends BaseServiceImpl<FileInfo> implements FileInfoService {

    private final FileInfoMapper fileInfoMapper;

    public FileInfoServiceImpl(MediaBaseMapper<FileInfo> baseMapper, FileInfoMapper fileInfoMapper) {
        super(baseMapper);
        this.fileInfoMapper = fileInfoMapper;
    }

    @Override
    public List<FileInfo> getFilesByCreateId(Integer createId) {
        return fileInfoMapper.selectByCreateId(createId);
    }

    @Override
    public List<FileInfo> getFilesByName(String fileName) {
        return fileInfoMapper.selectByFileName(fileName);
    }

    @Override
    public PageInfo<FileInfo> getFileListPage(Integer pageIndex, Integer pageSize, String fileName, Integer createId) {
        return PageHelper.startPage(pageIndex, pageSize, "create_time desc").doSelectPageInfo(() ->
                fileInfoMapper.selectFileList(fileName, createId)
        );
    }

    @Override
    public FileInfo saveFileInfo(FileInfo fileInfo) {
        fileInfoMapper.insertSelective(fileInfo);
        return fileInfo;
    }

    @Override
    public FileInfo getFileById(Integer fileId) {
        return fileInfoMapper.selectByPrimaryKey(fileId);
    }

    @Override
    public int deleteFile(Integer fileId) {
        return fileInfoMapper.deleteByPrimaryKey(fileId);
    }
}
